<?php
class Db_Table_TUser extends Db_TableAbstract
{
	const TABLE_NAME = 't_user';
	
	const HASH_ALGORITHM = 'sha256';
	
	/**
	 * ログインを試行し、正しければユーザーIDを返す
	 * @param string $loginId
	 * @param string $password
	 */
	public function getUserForLogin($loginId, $password)
	{
		$password = hash(self::HASH_ALGORITHM, $password);
		
		$select = $this->_db->select()
			->from(array('user_t' => self::TABLE_NAME), array(
				'user_id',
			))
			->where('BINARY user_t.login_id = ?', $loginId)
			->where('BINARY user_t.password_hash = ?', $password)
			->where('user_t.INVALID_FLG = ?', C_FLG_OFF);
		
		return $this->_db->fetchOne($select);
	}
	
	/**
	 * ログイン成功時のレコードを更新
	 * 
	 * @param int $userId
	 * @param int $ipAddr
	 * @param int $sessionId
	 * @return void
	 */
	public function updateUserByLogin($userId, $ipAddr, $sessionId)
	{
		$this->_db->update(self::TABLE_NAME, array(
				'last_login_date' => new Zend_Db_Expr('NOW()'),
				'last_login_ipaddr' => $ipAddr,
				'last_login_sessid' => $sessionId,
			), $this->_db->quoteInto('user_id = ?', $userId));
	}
	
	/**
	 * ユーザーコンテナの取得
	 * 
	 * @param int $user_id
	 * @return Container_UserFront
	 */
	public function getUserContainerByID($user_id)
	{
		$select = $this->_db->select()
			->from(array('user_t' => self::TABLE_NAME), array(
				'*',
		))
		->where('user_t.user_id = ?', $user_id)
		->where('user_t.INVALID_FLG = ?', C_FLG_OFF);
			
		$row = $this->_db->fetchRow($select);
		
		return new Container_UserFront($row);
	}
}